package com.bskj.sj.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.bskj.common.bean.model.BmsUser;
import com.bskj.common.bean.model.ShopCoupon;
import com.bskj.common.bean.model.UserCoupon;
import com.bskj.common.dao.ShopCouponDao;
import com.bskj.common.dao.UserCouponDao;
import com.bskj.common.dao.core.BaseDao;
import com.bskj.common.dao.core.FindRequestEntity;
import com.bskj.common.dao.core.OrderEnum;
import com.bskj.common.dao.core.WhereEnum;
import com.bskj.common.enums.BmsUserTypeEnum;
import com.bskj.common.enums.CouponLimitModeTypeEnum;
import com.bskj.common.enums.CouponStatusEnum;
import com.bskj.common.enums.CouponTimeTypeEnum;
import com.bskj.common.enums.ShopCouponStatusEnum;
import com.bskj.common.enums.ShopCouponValidTypeEnum;
import com.bskj.common.utils.DateTimeUtil;
import com.bskj.sj.bean.vo.resp.UserCouponRecordRespVo;
import com.bskj.sj.bean.vo.resp.UserCouponReleaseRespVo;
import com.bskj.sj.service.IShopCouponService;

/**
 * Created by mocaijin on 23/01/2018.
 */
@Service
public class ShopCouponServiceImpl extends BaseServiceImpl<ShopCoupon> implements IShopCouponService {
	@Autowired
	private ShopCouponDao shopCouponDao;

	@Autowired
	private UserCouponDao userCouponDao;
	

	@Override
	public BaseDao<ShopCoupon> getBaseDao() {
		return shopCouponDao;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void add(ShopCoupon shopCoupon) throws Exception {
		shopCouponDao.insert(shopCoupon);
	}

	@Override
	public void insert(BmsUser authInfo) throws Exception {

	}

	@Override
	public BmsUser getByCustId(int custId) throws Exception {
		return null;
	}

	@Override
	public List<ShopCoupon> get(ShopCoupon shopCoupon) throws Exception {
		return null;
	}

	@Override
	public List<ShopCoupon> getShopCoupons(ShopCoupon shopCoupon, Integer page, Integer pageSize) throws Exception {
		FindRequestEntity findRequestEntity = new FindRequestEntity();
		if (shopCoupon != null) {
			if (shopCoupon.getId() != null) {
				findRequestEntity.addWhere("id", WhereEnum.EQ, shopCoupon.getId());
			}
			if (shopCoupon.getParkingLotCustomerId() != null) {
				findRequestEntity.addWhere("parking_lot_customer_id", WhereEnum.EQ,
						shopCoupon.getParkingLotCustomerId());
			}
			findRequestEntity.addWhere("shop_id", WhereEnum.EQ , shopCoupon.getShopId());
		}
		
		findRequestEntity.addWhere("status", WhereEnum.EQ, ShopCouponStatusEnum.enable.getKey());
		findRequestEntity.addOrder("create_time", OrderEnum.DESC);
		findRequestEntity.setPage(page, pageSize);
		List<ShopCoupon> shopCouponList = find(findRequestEntity);
		return shopCouponList;
	}

	@Override
	public List<UserCouponReleaseRespVo> getDailyReleases(Integer parkingLotCustomerId, Date beginDate, Date endDate,BmsUser bmsUser)
			throws Exception {
		List<UserCouponReleaseRespVo> voList = new ArrayList<>();
			List<Map<String, Object>> mapList = userCouponDao.getDailyReleases(parkingLotCustomerId + "", beginDate,
					endDate);
			for (Map<String, Object> map : mapList) {
				UserCouponReleaseRespVo userCouponReleaseRespVo = new UserCouponReleaseRespVo();
				BeanUtils.populate(userCouponReleaseRespVo, map);
				voList.add(userCouponReleaseRespVo);
			}
			return voList;
	}

	@Override
	public List<UserCouponReleaseRespVo> getDailyTotal(Integer shopId, Integer type, Integer parkingLotCustomerId,
			Date beginDate, Date endDate) throws Exception {
		List<UserCouponReleaseRespVo> userCouponReleaseRespVos = new ArrayList<>();
		if (type ==BmsUserTypeEnum.parking_lot_user.getKey()) {
			UserCouponReleaseRespVo userCouponReleaseRespVo=new UserCouponReleaseRespVo();
			Integer usedCount = userCouponDao.getDailyTotal(parkingLotCustomerId + "",DateTimeUtil.date2String(beginDate), DateTimeUtil.date2String(endDate),null);
			userCouponReleaseRespVo.setUsedCount(usedCount);
			userCouponReleaseRespVos.add(userCouponReleaseRespVo);
			return userCouponReleaseRespVos;
		} else {
			return getCouponLimitMoney(shopId,parkingLotCustomerId,endDate);
		}
	}

	@Override
	public Date getValidDate(Integer id) throws Exception {
		ShopCoupon shopCoupon = get(id);
		if (shopCoupon.getValidType() == ShopCouponValidTypeEnum.coupon_crtain.getKey()) {
			double time = shopCoupon.getValidTime();
			int hour = (int) time;
			int min = (int) ((time - hour) * 60);
			long ms = hour * 60 * 60 * 1000 + min * 60 * 1000 + System.currentTimeMillis();
			Date date = new Date(ms);
			return date;
		} else {
			if ("24:00".equals(shopCoupon.getLastTime())) {
				String date = DateTimeUtil.date2String(new Date(), "yyyy-MM-dd") + " 23:59:59";
				return DateTimeUtil.string2Date(date);
			} else {
				String date = DateTimeUtil.date2String(new Date(), "yyyy-MM-dd") + " " + shopCoupon.getLastTime()
						+ ":00";
				return DateTimeUtil.string2Date(date);
			}

		}
	}
	
	public List<UserCouponReleaseRespVo> getCouponLimitMoney(Integer shopId, Integer parkingLotCustomerId,Date endDate) throws Exception {
		if(endDate == null){
			endDate = new Date();
		}
		List<UserCouponReleaseRespVo> userCouponReleaseRespVos = new ArrayList<>();
		FindRequestEntity findReq=new FindRequestEntity();
		findReq.addWhere("shop_id", WhereEnum.EQ, shopId);
		List<ShopCoupon> shopCoupons=shopCouponDao.find(findReq);
		for (ShopCoupon shopCoupon : shopCoupons) {
			UserCouponReleaseRespVo userCouponReleaseRespVo=new UserCouponReleaseRespVo();
			userCouponReleaseRespVo.setCouponMoneyType(shopCoupon.getCouponMoneyType());
			userCouponReleaseRespVo.setShopCouponName(shopCoupon.getMoney()/10+"元的停车券");
			userCouponReleaseRespVo.setTotalCount(shopCoupon.getCouponLimitNum());
			userCouponReleaseRespVo.setLimitModeType(shopCoupon.getLimitModeType());
			if(shopCoupon.getLimitModeType() == CouponLimitModeTypeEnum.Time_Number_Type.getKey()){
				if(shopCoupon.getCouponLimitType() == CouponTimeTypeEnum.every_day.getKey()){
				    Date beginDate = new Date();
				    beginDate = DateTimeUtil.string2Date(DateTimeUtil.date2String(new Date(),"yyyy-MM-dd")+" 00:00:00");
					int usedCount= userCouponDao.getDailyTotal(parkingLotCustomerId + "",  DateTimeUtil.date2String(beginDate), DateTimeUtil.date2String(endDate) ,shopCoupon.getId());
					userCouponReleaseRespVo.setCouponLimitType(CouponTimeTypeEnum.every_day.getValue());
					userCouponReleaseRespVo.setUsedCount(usedCount);
					userCouponReleaseRespVos.add(userCouponReleaseRespVo);
				}else if (shopCoupon.getCouponLimitType() == CouponTimeTypeEnum.every_week.getKey()) {
					SimpleDateFormat formater=new SimpleDateFormat("yyyy-MM-dd 00:00:00");
					Calendar cal=new GregorianCalendar();
					cal.setFirstDayOfWeek(Calendar.MONDAY);
					cal.setTime(new Date());
					cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek());
					Date first=cal.getTime();
					int usedCount = userCouponDao.getDailyTotal(parkingLotCustomerId + "",formater.format(first), DateTimeUtil.date2String(endDate),shopCoupon.getId());
					userCouponReleaseRespVo.setCouponLimitType(CouponTimeTypeEnum.every_week.getValue());
					userCouponReleaseRespVo.setUsedCount(usedCount);
					userCouponReleaseRespVos.add(userCouponReleaseRespVo);
				}else if (shopCoupon.getCouponLimitType() == CouponTimeTypeEnum.every_month.getKey()) {
					SimpleDateFormat formater=new SimpleDateFormat("yyyy-MM-dd 00:00:00");
					Calendar c = Calendar.getInstance();    
			        c.add(Calendar.MONTH, 0);
			        c.set(Calendar.DAY_OF_MONTH,1);
			        int usedCount = userCouponDao.getDailyTotal(parkingLotCustomerId + "",formater.format(c.getTime()),DateTimeUtil.date2String(endDate),shopCoupon.getId());
					userCouponReleaseRespVo.setCouponLimitType(CouponTimeTypeEnum.every_month.getValue());
					userCouponReleaseRespVo.setUsedCount(usedCount);
					userCouponReleaseRespVos.add(userCouponReleaseRespVo);
				}else if (shopCoupon.getCouponLimitType() == CouponTimeTypeEnum.annually.getKey()) {
					String startTime = DateTimeUtil.date2String(new Date(), "yyyy")+"-01-01 00:00:00";
					String endTime = DateTimeUtil.date2String(new Date(), "yyyy")+"-12-31 23:59:59";
					int usedCount = userCouponDao.getDailyTotal(shopCoupon.getParkingLotCustomerId() + "", startTime, endTime, shopCoupon.getId());
					userCouponReleaseRespVo.setCouponLimitType(CouponTimeTypeEnum.annually.getValue());
					userCouponReleaseRespVo.setUsedCount(usedCount);
					userCouponReleaseRespVos.add(userCouponReleaseRespVo);
				}
			}else if (shopCoupon.getLimitModeType() == CouponLimitModeTypeEnum.Inventory_Type.getKey()) {
				userCouponReleaseRespVo.setInventory(shopCoupon.getInventory());
				userCouponReleaseRespVos.add(userCouponReleaseRespVo);
			}
		}
		return userCouponReleaseRespVos;
	}

	@Override
	public List<UserCouponRecordRespVo> getShopReleases(Integer parkingLotCustomerId, Date beginDate, Date endDate,
			BmsUser bmsUser) throws Exception {
		List<UserCouponRecordRespVo> userCouponRecordRespVoList=new ArrayList<>();
		FindRequestEntity findReq=new FindRequestEntity();
		findReq.addWhere("shop_id", WhereEnum.EQ, bmsUser.getShopId());
		findReq.addWhere("create_time", WhereEnum.GT, beginDate);
		findReq.addWhere("create_time", WhereEnum.LT, endDate);
		findReq.addOrder("create_time", OrderEnum.ASC);
		List<UserCoupon> userCouponList=userCouponDao.find(findReq);
		for (UserCoupon userCoupon : userCouponList) {
			UserCouponRecordRespVo UserCouponRecordRespVo=new UserCouponRecordRespVo();
			UserCouponRecordRespVo.setId(userCoupon.getId());
			UserCouponRecordRespVo.setCreateTime(DateTimeUtil.date2String(userCoupon.getCreateTime()));
			UserCouponRecordRespVo.setMoney(userCoupon.getMoney());
			if(userCoupon.getStatus()==CouponStatusEnum.used.getKey()){
				UserCouponRecordRespVo.setStatus(CouponStatusEnum.used.getValue());
			}else {
				if(userCoupon.getValidDate().before(new Date())){
					UserCouponRecordRespVo.setStatus(CouponStatusEnum.invalid.getValue());
				}else {
					UserCouponRecordRespVo.setStatus(CouponStatusEnum.not_use.getValue());
				}
			}
			userCouponRecordRespVoList.add(UserCouponRecordRespVo);
		}
		return userCouponRecordRespVoList;
	}

	@Override
	public Integer getCouponMoney(Integer couponId) throws Exception {
		UserCoupon userCoupon=userCouponDao.get(couponId);
		return userCoupon.getMoney();
	}
	
	
}
